package com.amoyhong.hongWhether.service;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import com.amoyhong.hongWhether.events.ReceiveLocationEvent;
import com.orhanobut.logger.Logger;

import de.greenrobot.event.EventBus;

/**
 * Created by Administrator on 2016/2/20.
 */
@SuppressWarnings("MissingPermission")
public class LocateService extends Service {


    private LocationManager locationManager;
    private LocationListener locationListener;

    private static LocateService locateService;

    private boolean isStartLocating = false;

    public static LocateService getInstance() {
        return locateService;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        locateService = this;
        initPositionListener();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Logger.d("LocateService onStartCommand");
        startLocate();
        return super.onStartCommand(intent, flags, startId);
    }


    public void startLocate() {
        Criteria criteria = getCustomCriteria();
        locationManager.requestLocationUpdates(10000, 0, criteria, locationListener, null);
        isStartLocating = true;
    }


    private void stopLocate() {
        if (isStartLocating) {
            locationManager.removeUpdates(locationListener);
            isStartLocating = false;

            Logger.d("stopSelf");
            stopSelf();
        }

    }

    public boolean isStartLocating() {
        return isStartLocating;
    }


    @NonNull
    private Criteria getCustomCriteria() {
        Criteria criteria = new Criteria();

        criteria.setCostAllowed(true);
        criteria.setAltitudeRequired(true);
        criteria.setVerticalAccuracy(Criteria.ACCURACY_HIGH);
        criteria.setHorizontalAccuracy(Criteria.ACCURACY_HIGH);
        return criteria;
    }


    private void initPositionListener() {

        locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);

        locationListener = new LocationListener() {
            public void onLocationChanged(Location location) {
                Logger.d("onLocationChanged: " + location);

                EventBus.getDefault().post(new ReceiveLocationEvent(location));

                stopLocate();
            }

            public void onStatusChanged(String provider, int status, Bundle extras) {
            }

            public void onProviderEnabled(String provider) {
            }

            public void onProviderDisabled(String provider) {
            }
        };

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}
